package org.cmoine.perspectiveshare.perspectives;

import static org.eclipse.core.runtime.IStatus.ERROR;
import static org.eclipse.core.runtime.IStatus.INFO;

import org.cmoine.perspectiveshare.Activator;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

import bsh.EvalError;
import bsh.Interpreter;
import bsh.TargetError;

public class PerspectiveSharePerspectiveFactory implements IPerspectiveFactory {
    public static final String ID=PerspectiveSharePerspectiveFactory.class.getName();

    private static String code;

    public void createInitialLayout(IPageLayout layout) {
        try {
            Activator.log(INFO, "Interpret code:\n" + code, null); //$NON-NLS-1$
            Interpreter interpreter=new Interpreter();
            interpreter.set("layout", layout); //$NON-NLS-1$
            interpreter.eval("import org.eclipse.ui.IFolderLayout;\n" + code); //$NON-NLS-1$
            Activator.log(INFO, "Interpretation run OK", null); //$NON-NLS-1$
        } catch (TargetError e) {
            Activator.log(ERROR, "The script or code called by the script threw an exception: ", e); //$NON-NLS-1$
        } catch (EvalError e) {
            Activator.log(ERROR, "There was an error in evaluating the script:", e); //$NON-NLS-1$
        }
    }

    public static void setCode(String code) {
        PerspectiveSharePerspectiveFactory.code=code;
    }
}
